<?php

/**
 * @copyright       : FOSP
 * @version         : 20120323
 * @todo            : Access table player champion by MySQL
 * @name            : Fpt_Business_PlayerChampion_Adapter_Mysql
 * @author          : QuangTM
 */
class Fpt_Business_PlayerChampion_Adapter_Mysql extends Fpt_Business_PlayerChampion_Adapter_Abstract
{

    /**
     * Get Fpt_Business_PlayerChampion_Adapter_Mysql's instance
     * @return Fpt_Business_PlayerChampion_Adapter_Mysql
     * @author QuangTM
     */
    public static function getInstance ()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
        {
            self::$instance = new self;
        }
        return self::$instance;
    }

    /**
     * Get list top players by season
     * @param type $seasonId
     * @return array|boolean 
     * @author QuangTM
     */
    public function getListTopPlayers ($seasonId)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_fesport_getPlayerChampionBySeason(:p_season);');

            // Bind param
            $stmt->bindParam('p_season', $seasonId, PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Get result
            $result = array ();

            // Get format data
            $formatInstance = new Fpt_Business_PlayerChampion_Metadata_PlayerChampion();

            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                array_push($result, $formatInstance->init($row)->getFormatedData());
            }

            // Close
            $stmt->closeCursor();

            // Release variables
            unset($stmt, $formatInstance);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}